package com.sky.controller.user;

import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.service.ShoppingCartService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("userOrderController")
@RequestMapping("/user/order")
@Api(tags = "用户端订单接口")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        OrderSubmitVO orderSubmitVO = orderService.submit(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    @GetMapping("/historyOrders")
    public Result<PageResult> page(int page, int pageSize, Integer status){
        log.info("用户历史订单查询");
        PageResult pageResult = orderService.pageQuery(page,pageSize,status);
        return Result.success(pageResult);
    }


    /**
     * 根据订单id查询详情 for[回显]
     * @param orderId
     * @return
     */
    @GetMapping("/orderDetail/{orderId}")
    public Result<OrderVO> orderDetail(@PathVariable Long orderId){
        OrderVO orderVO = orderService.orderDetail(orderId);
        return Result.success(orderVO);
    }

    /**
     * c-端 根据订单id取消订单
     * @param id
     * @return
     */
    @PutMapping("/cancel/{id}")
    public Result orderCancel(@PathVariable Long id){
        log.info("我准备取消订单啦");
        orderService.orderCancel(id);
        return Result.success();
    }

    /**
     * c-端 再来一单
     * 效果是跳转到菜单界面，然后给这个id的用户购物车重新恢复
     * 实际上就是添加购物车的功能
     * @param id
     * @return
     */
    @PostMapping("/repetition/{id}")
    public Result oneMoreOrder(@PathVariable Long id){
        orderService.oneMoreOrder(id);
        return Result.success();
    }
}
